package com.tencent.welink.api.msg;

import com.alibaba.fastjson.JSONObject;
import com.tencent.welink.api.OpenApiTokenChange;
import com.tencent.welink.api.base.WelinkOpenApi;
import com.tencent.welink.config.BussinessConfig;
import com.tencent.welink.util.CrontabUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;

/**
 * Created by limye on 2020/6/4.
 * desc:发送消息到设备,同时让设备将该消息透传到应用（通过websocket监听可以接收到）
 */
public class ToDeviceMsgPassThrough extends WelinkOpenApi {

    private static final Logger log = LogManager.getLogger(ToDeviceMsgPassThrough.class);
    public ToDeviceMsgPassThrough() {
        super("/common/msg/send",true);
    }

    @Override
    public void displayResult(String result) {

    }

    public static void main(String[] args) {
        new BussinessConfig();
        CrontabUtil.addTaskInRate(new OpenApiTokenChange(), BussinessConfig.loginDurationMinutes,true);
        ToDeviceMsgPassThrough toDeviceMsgSend =   new ToDeviceMsgPassThrough();
        Map<String,Object> map = toDeviceMsgSend.getExtendsPara();

        String din = "300000000000073213";

        //parking/in
        sendParkingInMsg(map,din);

        //车位状态/violate
        //sendParkingViolateMsg(map,din);


        //人脸闸机in
        //sendFaceAccessMsg(map,din);

        //门禁in
        //sendAccessControlMsg(map,din);

        toDeviceMsgSend.DoHttp();
        CrontabUtil.shutdown();
    }

    /**
     * parking/in
     * @param map
     */
    public static void sendParkingInMsg(Map<String,Object> map,String din){
        //透传datapoint为-1
        map.put("datapoint",-1);
        //透传消息对应的设备
        //停车
        map.put("din",din);

        JSONObject jsonObject = new JSONObject();

        //实际网关上传的datapoint
        jsonObject.put("datapoint",10086);

        //需要透传的数据
        //parking/in
        String value = "{" +
                "\"profile\": {" +
                "\"modelId\": 100," +
                "\"appType\": \"parking\"," +
                "\"poiCode\": \"t0401001\"" +
                "}," +
                "\"reportTs\": 1593401660973," +
                "\"properties\": {" +
                "}," +
                "\"events\": {" +
                "\"in\": {" +
                "\"laneType\": 1," +
                "\"image\": {" +
                "" +
                "}," +
                "\"carType\": 1," +
                "\"parkingLotId\": 0," +
                "\"permission\": 1," +
                "\"eventType\": 1," +
                "\"plateNum\": \"B12345\"," +
                "\"eventTs\": 1593401660973," +
                "\"vehicleType\": 1," +
                "\"parkingLotName\": \"停车场\"," +
                "\"define\": [\"xxx\",\"xxx2\"]," +
                "\"exitName\": \"出口1\"" +
                "}" +
                "}" +
                "}";

        jsonObject.put("value",value);

        map.put("cmd", jsonObject.toJSONString());
    }

    /**
     * 车位状态/violate
     * @param map
     */
    public static void sendParkingViolateMsg(Map<String,Object> map,String din){
        //透传datapoint为-1
        map.put("datapoint",-1);
        //透传消息对应的设备
        //停车
        map.put("din",din);

        JSONObject jsonObject = new JSONObject();

        //实际网关上传的datapoint
        jsonObject.put("datapoint",10087);

        //需要透传的数据
        //parking/in
        String value = "{" +
                "\"profile\": {" +
                "\"modelId\": 100," +
                "\"appType\": \"parking\"," +
                "\"poiCode\": \"t0401005\"" +
                "}," +
                "\"reportTs\": 1593401660973," +
                "\"properties\": {" +
                "}," +
                "\"events\": {" +
                        "\"violate\": {" +
                                "\"parkingLotId\": 0," +
                                "\"eventType\": 2," +
                                "\"plateNumber\": \"B12345\"," +
                                "\"eventTs\": 1593401660973," +
                                "\"placeStatus\": 2," +
                                "\"placeType\": 1," +
                                "\"placeId\": \"001\"," +
                                "\"placeName\": \"车位1\"," +
                                "\"parkingLotName\": \"停车场\"" +
                           "}" +
                      "}" +
                "}";

        jsonObject.put("value",value);

        map.put("cmd", jsonObject.toJSONString());
    }


    /**
     * 人脸门禁in
     * @param map
     */
    public static void sendFaceAccessMsg(Map<String,Object> map,String din){
        //透传datapoint为-1
        map.put("datapoint",-1);
        //透传消息对应的设备
        //停车
        map.put("din",din);

        JSONObject jsonObject = new JSONObject();

        //实际网关上传的datapoint
        jsonObject.put("datapoint",100087);

        //需要透传的数据
        //face_access/in
        String value = "{" +

                "\"profile\": {" +
                  "\"modelId\": 100," +
                  "\"appType\": \"face_access\"," +
                  "\"poiCode\": \"t0601001\"" +
                  "}," +

                "\"reportTs\": "+System.currentTimeMillis()+"," +
                  "\"properties\": {" +
                 "}," +

                "\"events\": {" +
                  "\"in\": {" +
                      "\"eventTs\": "+System.currentTimeMillis()+"," +
                      "\"eventType\": 1," +
                      "\"personInfo\": {" +
                            "\"personName\": \"yedonglin888\"," +
                            "\"personType\": 2," +
                            "\"recognitionId\": \"1\"" +
                          "}," +
                        "\"position\": {" +
                                      "}," +
                        "\"permission\": 1," +
                        "\"score\": 1," +
                        "\"image\": {" +
                            "\"imageType\": 2," +
                            "\"imageData\": \"www.download.com\"" +
                            "}" +
                      "}" +
                  "}" +


                "}";

        System.out.println(value);
        JSONObject jsonObject1 = JSONObject.parseObject(value);
        jsonObject.put("value",value);

        map.put("cmd", jsonObject.toJSONString());
    }




    /**
     * 门禁in
     * @param map
     */
    public static void sendAccessControlMsg(Map<String,Object> map,String din){
        //透传datapoint为-1
        map.put("datapoint",-1);
        //透传消息对应的设备
        //停车
        map.put("din",din);

        JSONObject jsonObject = new JSONObject();

        //实际网关上传的datapoint
        jsonObject.put("datapoint",100088);

        //需要透传的数据
        //face_access/in
        String value = "{" +

                "\"profile\": {" +
                "\"modelId\": 100," +
                "\"appType\": \"access_control\"," +
                "\"poiCode\": \"t1101001\"" +
                "}," +

                "\"reportTs\": "+System.currentTimeMillis()+"," +
                "\"properties\": {" +
                "}," +

                "\"events\": {" +
                        "\"in\":{" +
                                "\"eventTs\": 148813512323," +
                                "\"eventType\":1," +
                                "\"personInfo\": {" +
                                        "\"personName\": \"limye\"," +
                                        "\"personType\": 1," +
                                        "\"recognitionId\": \"1\"" +
                                "}," +
                                "\"position\": {" +
                                "}," +
                                "\"permission\": 1," +
                                "\"accessType\": 1," +
                               "\"media\": {" +
                                    "\"mediaType\": 2," +
                                    "\"mediaData\": \"www.download.com\"" +
                                "}" +
                        "}" +
                "}" +


                "}";

        System.out.println(value);


        jsonObject.put("value",value);

        map.put("cmd", jsonObject.toJSONString());
    }

}
